package com.rivulet.doj_backend.judger.judge1_0;

import com.rivulet.doj_backend.pojo.JudgeCode;
import com.rivulet.doj_backend.tools.CommandUtil;
import com.rivulet.doj_backend.tools.FileUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

/**
 * @desc: 编译模块V2
 * @author xjc
 */
public class JudgeNumTwo {
    public static String fileName = "D:\\doj_workspace";
    public void judge(String userId, String code, String languageType, List<JudgeCode> list) {
        String workPath = FileUtils.getWorkPath(CommandUtil.OSType);

        String[] command = CommandUtil.chooseJudgeCommand(workPath,languageType);
        try {
            for (int i = 0; i < 5; i++) {
                Date beginTime = new Date();
                Process process = Runtime.getRuntime().exec(command, null, new File(fileName));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
                writer.write(list.get(i).getWriterParam());
                writer.close();
                SequenceInputStream allInputStream = new SequenceInputStream(process.getInputStream(), process.getErrorStream());
                InputStreamReader reader = new InputStreamReader(allInputStream, "gbk");
                BufferedReader bufferedReader = new BufferedReader(reader);
                String res = "";
                StringBuilder str = new StringBuilder();
                while ((res = bufferedReader.readLine()) != null) {
                    System.out.println(res);
                    str.append(res + "\n");
                }
                bufferedReader.close();
                process.waitFor();
                process.destroy();
                Date endTime = new Date();
                long time = endTime.getTime() - beginTime.getTime();
                list.get(i).setReader(str.toString());
                list.get(i).setRunningTime((int) time);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            //deleteDir(new File(fileName));
        }
    }

}
